package com.md.youyouhui.ui.pay;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivitySelectCouponBinding;
import com.md.youyouhui.databinding.AdapterSelectCouponBinding;
import com.md.youyouhui.net.response.CouponAvailableResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.ui.adapter.DataBoundViewHolder;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/4/28.
 */
public class SelectCouponActivity extends BaseActivity {
    private ActivitySelectCouponBinding couponBinding;
    private MainViewModel viewModel;
    private MyAdapter adapter;

    @Override
    public void setContentLayout() {
        couponBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_coupon);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(couponBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        String id = getIntent().getStringExtra("id");
        String price = getIntent().getStringExtra("price");
        getCouponAvailable(id, price);
        adapter = new MyAdapter();
        couponBinding.couponRecycler.setAdapter(adapter);
    }

    private void getCouponAvailable(String id, String price) {
        viewModel.couponAvailable(id, price).observe(this, couponAvailableResponse -> {
            assert couponAvailableResponse != null;
            switch (couponAvailableResponse.status) {
                case LOADING:
                    couponBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    couponBinding.progress.setVisibility(View.GONE);
                    List<CouponAvailableResponse> data = couponAvailableResponse.data;
                    if (null == data) {
                        return;
                    }
                    if (data.size() == 0) {
                        Toast.makeText(this, "暂无可用的优惠券", Toast.LENGTH_SHORT).show();
                    }
                    adapter.upDate(data);
                    break;
                case ERROR:
                    couponBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, couponAvailableResponse.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterSelectCouponBinding>> {
        List<CouponAvailableResponse> rows;

        public void upDate(List<CouponAvailableResponse> rows) {
            this.rows = rows;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public DataBoundViewHolder<AdapterSelectCouponBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            AdapterSelectCouponBinding userBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.adapter_select_coupon, viewGroup, false);
            return new DataBoundViewHolder<>(userBinding);
        }

        @Override
        public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterSelectCouponBinding> holder, int i) {
            //useType 0有门槛,1无门槛
            holder.binding.couponPrice.setText(rows.get(i).amount);
            holder.binding.couponDay.setText(String.format("有效期至%s", rows.get(i).overdueTime));
            holder.itemView.setOnClickListener(v -> {
                Intent intent = new Intent();
                intent.putExtra("id", rows.get(i).id);
                intent.putExtra("price", rows.get(i).amount);
                setResult(RESULT_OK, intent);
                finish();
            });

        }

        @Override
        public int getItemCount() {
            return null == rows ? 0 : rows.size();
        }
    }
}
